var express = require('express');
const { default: mongoose } = require('mongoose');
var router = express.Router();
const { notifiCationModel, journalismModel, noticeClassfyModel } = require('../model/hyq_model')
router.get('/region', async (req, res) => {
	res.send({})
})

// 接受通知公告列表
router.get('/get_notification', async (req, res) => {
	let pw = req.query
	let data = await notifiCationModel.find()
	if (pw.pw) {
		let a = '/' + pw.pw + '/'
		data = await notifiCationModel.find({ "title": { $regex: eval(a) } })
	}
	if (pw.classfy) {
		data = await notifiCationModel.find({ noticeClassId: pw.classfy })
	}
	res.send({
		code: 200,
		data,
		msg: '成功接受'
	})
})
// 添加通知公告
router.post('/add_notification', async (req, res) => {
	let data = req.body
	// console.log(data)
	await notifiCationModel.create(data)
	res.send({
		code: 200,
		msg: '添加公告成功'
	})
})

// 接受通知图片新闻
router.get('/get_journalism', async (req, res) => {
	let data = await journalismModel.find()
	res.send({
		code: 200,
		data,
		msg: '成功接受'
	})
})
// 通知公告分类获取
router.get('/get_noticeClassFy', async (req, res) => {
	let data = await noticeClassfyModel.aggregate([
		{
			$lookup: {
				from: "notificationfy",
				localField: "_id",
				foreignField: "noticeId",
				as: "children",
				pipeline: [
					{
						$lookup: {
							from: "notificationfy",
							localField: "_id",
							foreignField: "noticeId",
							as: "children",
							pipeline: [
								{
									$lookup: {
										from: "notificationfy",
										localField: "_id",
										foreignField: "noticeId",
										as: "children",
									}
								}
							]
						}
					}
				]
			},
		},
		{
			$match: { level: 1 }
		}
	])
	res.send({
		code: 200,
		data
	})
})
// 添加公告
router.post('/addNotice', async (req, res) => {
	let data = req.body
	console.log(data, '添加data')
	await notifiCationModel.create(data)
	res.send({
		code: 200,
		msg: "添加成功"
	})
})
// 查询某个通知
router.post('/getone', async (req, res) => {
	let key = req.body.key
	let data = await notifiCationModel.find({ _id: key })
	let namesort = await noticeClassfyModel.find({ _id: data[0].noticeClassId })
	res.send({
		code: 200,
		data,
		namesort
	})
})
// 修改某个通知
router.post('/editnotice', async (req, res) => {
	let data = req.body
	// console.log(data)
	await notifiCationModel.updateOne({ _id: data._id }, {
		title: data.title,
		noticeClassId: data.noticeClassId,
		isstate: data.isstate,
		reveive: data.reveive,
		content: data.content
	})
	res.send({
		code: 200,
		msg: "修改成功"
	})
})
// 删除通知
router.post('/delNotice', async (req, res) => {
	let data = req.body.key
	console.log(data)
	await notifiCationModel.deleteOne({ _id: data })
	res.send({
		code: 200,
		msg: "删除成功"
	})
})
// 增加分类
router.post('/addClassfy', async (req, res) => {
	let data = req.body
	// console.log(data)
	let upLevel = await noticeClassfyModel.find({ _id: data.ValInfo })
	const oneInfo = {
		classfyName: data.classfyName,
		level: upLevel[0].level + 1,
		noticeId: data.ValInfo
	}
	// console.log(oneInfo,'最终添加')
	await noticeClassfyModel.create(oneInfo)
	res.send({
		code: 200,
		msg: "添加成功"
	})
})
// 修改通知分类
router.post('/editClassfy', async (req, res) => {
	let data = req.body
	let b = await noticeClassfyModel.find({ _id: data.noticeId })
	await noticeClassfyModel.updateOne({ _id: data.ValInfo }, { classfyName: data.classfyName, noticeId: data.noticeId, level: b[0].level + 1 })
	res.send({
		code: 200,
		msg: "修改分类成功"
	})
})
// 删除通知分类
router.post('/delClasssfy', async (req, res) => {
	let key = req.body.ValInfo
	await noticeClassfyModel.deleteOne({ _id: key })
	res.send({
		code: 200,
		msg: "删除成功"
	})

})
// 获取新闻内容
router.get('/getJournalism', async (req, res) => {
	let data = await journalismModel.find()
	let pw = req.query
	// console.log(pw, '获取到的')
	if (pw.pw) {
		let a = '/' + pw.pw + '/'
		data = await journalismModel.find({ "title": { $regex: eval(a) } })
	}
	res.send({
		code: 200,
		data
	})
})
// 添加新闻
router.post('/addJournalism',async(req,res)=>{
	let data = req.body
	// console.log(data)
	await journalismModel.create(data)
	res.send({
		code:200,
		msg:"添加新闻成功"
	})
})
// 查找某一个新闻
router.post('/getJournalism_one', async (req, res) => {
	let pw = req.body.key
	let data = await journalismModel.find({_id:pw})
	// console.log(data, '获取到的')
	res.send({
		code: 200,
		data
	})
})
// 修改新闻
router.post('/edit_journalism',async(req,res)=>{
	let data = req.body
	// console.log(data,'获取到底的社会局')
	await journalismModel.updateOne({_id:data.id},{title:data.title,publish:data.publish,content:data.content,title_pic:data.title_pic})
	res.send({
		code:200,
		msg:"修改成功"
	})
})

module.exports = router;